/**
 * 权限管理
 */
var rights = function() {
    var self = this;
    self.uriNames = {};
    self.rights = {};
}
rights.prototype = {};

rights.prototype.init = function(cb) {
    var self = this;
    var menus = window.menus;
    for (var i in menus) {
        var m1 = menus[i];
        var submenus = m1.left;
        var rights = m1.rights;
        if (!Util.isEmpty(rights)) {
            for (var k in rights) {
                self.add(rights[k].uri, rights[k].name);
            }
        }

        for (var j in submenus) {
            var m2 = submenus[j];
            var rights = m2.rights;
            if (typeof(rights) == 'string') continue;
            if (Util.isEmpty(rights)) continue;
            for (var k in rights) {
                self.add(rights[k].uri, rights[k].name);
            }
        }
    }

    CMD.crosier_query(function(r) {
        // 保存所有的权限配置
        for (var i in r) {
            self.rights[r[i].uri] = r[i].grades.split(",");
        }

        // 获取角色配置信息
        self.roles = {};
        Classify.queryExt("roles", "idname", true, {}, function(r) {
            self.roles = r;
            if (cb) cb();
        });
    });
}

rights.prototype.check = function(uri, grade) {
    var self = this;
    grade = grade + "";
    var arr = uri.split(",");
    if (grade == "99") return true;
    if (grade === undefined)
        grade = context.user.user.grade + '';

    for (var i in arr) {
        var u = arr[i];
        if (Util.isEmpty(self.rights[u])) return false;
        if (self.rights[u].indexOf(grade) == -1) return false;
    }
    return true;
}

rights.prototype.add = function(uri, name) {
    var self = this;
    var arr = uri.split(",");
    for (var i in arr) {
        self.uriNames[arr[i]] = name;
    }
}

// 生成一颗权限树
rights.prototype.tree = function(grade) {
    var self = this;
    grade = grade + '';
    var list = [];
    var menus = window.menus;
    for (var i in menus) {
        var m1 = menus[i];
        if (window.bocai && m1.cp == true) continue;
        if (!window.bocai && m1.wx == true) continue;

        // 第一级加进来
        list.push({
            id: m1.name,
            name: m1.name,
            open: true,
        });
        var rights = m1.rights;
        if (!Util.isEmpty(rights)) {
            for (var k in rights) {
                list.push({
                    pId: m1.name,
                    id: rights[k].uri,
                    name: rights[k].name,
                    cmd: rights[k].uri,
                    checked: self.check(rights[k].uri, grade),
                });
            }
        }

        // 添加所有子项目
        var submenus = m1.left;
        for (var j in submenus) {
            var m2 = submenus[j];
            if (window.bocai && m2.cp == true) continue;
            if (!window.bocai && m2.wx == true) continue;

            var rights = m2.rights;
            if (typeof(rights) == 'string') continue;
            if (Util.isEmpty(rights)) continue;

            var pid = m2.name;
            if (rights.length > 1) {
                list.push({
                    pId: m1.name,
                    id: m2.name,
                    name: m2.name,
                    open: true,
                });
            }
            else {
                pid = m1.name;
            }
            for (var k in rights) {
                list.push({
                    pId: m1.name,
                    id: rights[k].uri,
                    name: rights[k].name,
                    pId: pid,
                    cmd: rights[k].uri,
                    checked: self.check(rights[k].uri, grade),
                });
            }
        }
    }
    return list;
}

// 保存权限
rights.prototype.save = function(grade, m) {
    var self = this;
    grade = grade + '';
    for (var k in self.rights) {
        var list = self.rights[k];
        if (m.indexOf(k) == -1) {
            // 干掉权限
            var index = list.indexOf(grade);
            if (index != -1) list.splice(index, 1);
        }
    }
    for (var i in m) {
        var k = m[i];
        if (!self.rights[k]) self.rights[k] = [grade];
        else if (self.rights[k].indexOf(grade) == -1) {
            self.rights[k].push(grade);
        }
    }

    var toRemoveList = [];
    var toSaveList = {};
    for (var k in self.rights) {
        if (self.rights[k].length == 0) toRemoveList.push(k);
        else {
            toSaveList["uri" + k] = self.rights[k].join(",");
        }
    }
    if (!isEmpty(toRemoveList)) {
        CMD.crosier_delete(toRemoveList.join(","));
    }

    // 添加额外的权限
    var t = ''; for (var i = 50; i <= 98; i++) t = t + i + ',';
    toSaveList["uri" + "/user/query"] = t;
    
    if (!isEmpty(toSaveList)) {
        startLoading();
        CMD.crosier_save(toSaveList, function() {
            stopLoading();
        });
    }
}

window.Rights = new rights();
